// pages/user/special/special.js
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: '',
    index: '',
    tabs: [{
      key: 'tab1',
      title: '可参加',
    },
    {
      key: 'tab2',
      title: '已申请',
    }
    ],
    joinList:[],
    isLoadedJoinList: false,
    isFirstTipJoin: true,
    pageJoin: 1,

    appliedSpecialList:[],
    isLoadedApplied: false,
    isFirstTipApplied: true,
    pageApplied: 1,

    pageSize:10,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    this.getJoinList();
    this.getAppliedList();
  },

  onTabsChange(e) {
    const {
      key
    } = e.detail;
    const index = this.data.tabs.map((n) => n.key).indexOf(key);

    this.setData({
      key,
      index,
    });
  },

  onSwiperChange(e) {
    const {
      current: index,
      source
    } = e.detail
    const {
      key
    } = this.data.tabs[index]

    if (!!source) {
      this.setData({
        key,
        index,
      })
    }
  },

  getJoinList() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'special_activity/list/can_join',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pageJoin,
        size: this.data.pageSize
      },
      success: (res) => {
        if (res.rowCount == this.data.joinList.length){
          this.isLoadedJoinList = true;
          return 
        }
        this.setData({
          joinList: this.data.joinList.concat(res.list)
        });
      }
    })
  },

  getAppliedList() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'special_activity/list/applyed',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pageApplied,
        size: this.data.pageSize,
        companyId: getApp().globalData.userInfo.companyId
      },
      success: (res) => {
        if (res.rowCount == this.data.appliedSpecialList.length) {
          this.isLoadedApplied = true;
          return
        }
        this.setData({
          appliedSpecialList: this.data.appliedSpecialList.concat(res.list)
        });
      }
    })
  },

  onLoadMoreJoinList() {
    if (this.data.isLoadedJoinList) {
      if (this.data.isFirstTipJoin) {
        this.toast('已加载完全部数据了');
      }
      this.data.isFirstTipJoin = false;
      return;
    }
    this.data.pageJoin += 1;
    this.getJoinList();
  },
  onLoadMoreAppliedList() {
    if (this.data.isLoadedApplied) {
      if (this.data.isFirstTipApplied) {
        this.toast('已加载完全部数据了');
      }
      this.data.isFirstTipApplied = false;
      return;
    }
    this.data.pageApplied += 1;
    this.getAppliedList();
  },

  onJumbDetail(e) {
    let param = {
      id: e.currentTarget.dataset.id,
      type: e.currentTarget.dataset.type
    }
    wx.navigateTo({
      url: './detail/detail?' + T.param(param),
    })
  }
})